<blockquote class="layui-elem-quote">
    <div class="layui-inline">
        <i class="layui-icon layui-icon-list layui-head-icon"></i>
        <span class="layui-font-16 layui-font-green">优惠券管理</span>
    </div>
    <div class="layui-pull-right">
        <a class="layui-btn layui-btn-primary layui-border-green" href="{:url('/store/coupon/create')}">添加优惠券</a>
    </div>
</blockquote>
<div class="layui-card">
    <div class="layui-card-body">
        <fieldset class="layui-elem-field">
            <legend>条件搜索</legend>
            <div class="layui-field-box">
                <form class="layui-form layui-form-pane" action="{:url('/store/coupon')}" method="GET">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">商品状态</label>
                            <div class="layui-input-inline">
                                <select name="status">
                                    <option value="">请选择状态</option>
                                    <option value="1">上架</option>
                                    <option value="0">下架</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">优惠券名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="keyword"  placeholder="请输入优惠名称" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <button  class="layui-btn" lay-submit lay-filter="search">搜索</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <form class="layui-form">
            <table class="layui-table">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>优惠券图标</th>
                    <th>优惠券类型</th>
                    <th>优惠券名称</th>
                    <th>数量</th>
                    <th>发放数量</th>
                    <th>使用数量</th>
                    <th>未使用数量</th>
                    <th>状态</th>
                    <th>创建时间</th>
                    <th>更新时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>

                {foreach $list as $vo}
                <tr>
                    <td>{$vo.id}</td>
                    <td><img src="{$vo.icon}" class="layui-view-img"></td>
                    <td>{$vo.coupon_type}</td>
                    <td>{$vo.coupon_name}</td>
                    <td>{$vo.quota}</td>
                    <td>{$vo.grant_count}</td>
                    <td>{$vo.used_count}</td>
                    <td>{$vo.grant_count-$vo.used_count}</td>
                    <td>
                        {switch $vo.status}
                        {case 0} <span class="layui-font-red">禁用</span> {/case}
                        {case 1} <span class="layui-font-green">启用</span> {/case}
                        {case -1} <span class="layui-font-yellow">已失效</span> {/case}
                        {/switch}
                    </td>
                    <td>{$vo.create_time}</td>
                    <td>{$vo.update_time}</td>
                    <td>
                        <a class="layui-btn layui-btn-sm layui-btn-primary layui-border-green choose-user">发放优惠券</a>
                        <a class="layui-btn layui-btn-sm" href="{:url('store/coupon/edit',['id'=> $vo.id])}">编辑</a>
                        <a class="layui-btn layui-btn-sm layui-btn-danger del" data-url="{:url('/store/coupon/delete',['id'=> $vo.id])}">删除</a>
                    </td>
                </tr>
                {/foreach}
                </tbody>
            </table>
        </form>
        {$list|raw}
    </div>
</div>
<script>
    $('.choose-user').click(function () {
        layer.open({
            title: ['选择会员'],
            type: 2,
            area: ['1000px', '800px'],
            offset: '30px',
            btn: ['确定', '取消'],
            maxmin: true,
            content: '/store/user/option',
            yes: function (index, layero) {
                let checkbox = $(layero).find('iframe').contents().find('.son:checked');
                if (checkbox.length < 1) {
                    layer.msg('请选择商品!', {icon: 2});
                    return false;
                }

                let goodsInfo = [];


                checkbox.each(function () {
                    goodsInfo.push({
                        goods_id: $(this).val(),
                        goods_name: $(this).parents('tr').find('.goods-name').text(),
                        main_image: $(this).parents('tr').find('.main-image').attr('src')
                    });
                })
                let param = [];

                let paramEle = $('#param');
                param = paramEle.val() ? JSON.parse(paramEle.val()).concat(goodsInfo) : goodsInfo;

                getUseGoodsInfo(param);

                parent.layer.close(index);
            }
        });

    });
</script>